﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using WFNetLib.PacketProc;
using DAL;
using System.Threading;
using System.Media;
using NationalInstruments.UI.WindowsForms;

namespace 发电机舱
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static void AlarmProc() 
        {
            SoundPlayer sp = new SoundPlayer();
            sp.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\Alarm.wav";
            while (bRun)
            {
                if (AlarmByte1 != 0 || AlarmByte2!=0)
                    sp.PlaySync();
                for (int i = 0; i < 2000; i++)
                {
                    Thread.Sleep(1);
                    if (!bRun)
                        return;
                }                 
            }
        }
        static bool bRun=true;
        Form2 fdjUI1, fdjUI2;
        fdjData fdjData1, fdjData2;
        bool bCOM1 = true, bCOM2 = true, bCOM5 = true, bCOM6 = true;
        bool bAlarmSave1 = true;
        bool bAlarmSave2 = true;
        int ModbusRxTimeOut = 500;
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(AlarmProc));
            t.Start();
            WFNetLib.ADO.SQLServerOP.ConnectionString = @"Data Source=.\SQL2008;Initial Catalog=CYFDJXT;Persist Security Info=True;User ID=sa;Password=1001001";
            fdjData1 = new fdjData();
            fdjData2 = new fdjData();
            if (!WFGlobal.OpenSerialPort(ref serialPort1, "转速1传感器串口"))
            {
                bCOM1 = false;
            }
            if (!WFGlobal.OpenSerialPort(ref serialPort2, "转速2传感器串口"))
            {
                bCOM2 = false;
            }
            if (!WFGlobal.OpenSerialPort(ref serialPort3, "温度压力传感器串口"))
            {
                bCOM5 = false;
            }
            if (!WFGlobal.OpenSerialPort(ref serialPort4, "电表传感器串口"))
            {
                bCOM6 = false;
            }
            fdjUI1 = new Form2(this);
            fdjUI1.UIIndex = 1;
            fdjUI1.Location = new Point(2464, 123);
            fdjUI1.Show();
            fdjUI2 = new Form2(this);
            fdjUI2.UIIndex = 2;
            fdjUI2.Location = new Point(-1500, 123);
            fdjUI2.Show();
            //Thread.Sleep(2000);
            fdjUI1.WindowState = FormWindowState.Maximized;
            fdjUI2.WindowState = FormWindowState.Maximized;
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出此软件么？", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                bRun = false;
                this.Close();
            }
        }

        bool bAlarm1=false;
        bool bAlarm2 = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            RTUModbus_Master modbus = new RTUModbus_Master(serialPort2);
            modbus.RetryTimes = 1;
            modbus.RxTimeOut = ModbusRxTimeOut;
            modbus.RetryInterval = 1;
            modbus.TargetAddr = 0x01;
            byte[] rxList;
            if(cyj1Check.Checked)
            {
                if (bCOM1)
                {
                    if (modbus.ProcCommand03(0x007c, 0x0002, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            fdjData1.ZS = BytesOP.MakeInt(rxList[3], rxList[4], rxList[5], rxList[6]);//AN0
                            fdjData1.ZS = fdjData1.ZS * 0.05;
                            tbZS.Text = fdjData1.ZS.ToString("f1");
                            gZS.Value = fdjData1.ZS;
                        }));
                        fdjUI1.Form2_ProcZS(rxList);
                    }
                }
            }
            RTUModbus_Master modbus1 = new RTUModbus_Master(serialPort1);
            modbus1.RetryTimes = 1;
            modbus1.RxTimeOut = ModbusRxTimeOut;
            modbus1.RetryInterval = 1;
            modbus1.TargetAddr = 0x01;
            byte[] rxList1;
            if(cyj2Check.Checked)
            {
                if (bCOM2)
                {
                    if (modbus1.ProcCommand03(0x007c, 0x0002, out rxList1))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            fdjData2.ZS = BytesOP.MakeInt(rxList1[3], rxList1[4], rxList1[5], rxList1[6]);//AN0
                            fdjData2.ZS = fdjData2.ZS * 0.05;
                            tbZS1.Text = fdjData2.ZS.ToString("f1");
                            gZS1.Value = fdjData2.ZS;
                        }));
                        fdjUI2.Form2_ProcZS(rxList1);
                    }
                }
            }
            timer1.Enabled = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            byte[] rxList;
            timer2.Enabled = false;
            RTUModbus_Master modbus = new RTUModbus_Master(serialPort3);
            modbus.RetryTimes = 1;
            modbus.RxTimeOut = ModbusRxTimeOut;
            modbus.RetryInterval = 1;
            if(bCOM5)
            {
                if (cyj1Check.Checked)
                {
                    modbus.TargetAddr = 0x04;
                    if (modbus.ProcCommand04(0x0040, 0x0008, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            double f = BytesOP.MakeShort(rxList[5], rxList[6]);//AN0
                            f = 0.0001 * (f - 500);
                            fdjData1.JYYL = f;
                            CheckYLAlarm(1, 0, tbJYYL, f);
                            tbJYYL.Text = f.ToString("f3");
                            gJYYL.Value = f;

                            f = BytesOP.MakeShort(rxList[3], rxList[4]);//AN1
                            f = 0.00025 * (f - 500);
                            f = 0.1;
                            fdjData1.RYYL = f;
                            CheckYLAlarm(1, 1, tbRYYL, f);
                            tbRYYL.Text = f.ToString("f3");
                            gRYYL.Value = f;

                            f = BytesOP.MakeShort(rxList[7], rxList[8]);//AN2
                            f = 0.0001 * (f - 500);
                            fdjData1.LQSYL = f;
                            CheckYLAlarm(1, 2, tbLQSYL, f);
                            tbLQSYL.Text = f.ToString("f3");
                            gLQSYL.Value = f;

                            f = BytesOP.MakeShort(rxList[9], rxList[10]);//AN3
                            f = 0.05 * (f - 500);
                            fdjData1.LQSWD = f;
                            CheckT1Alarm(1, 3, tbLQSWD, f);
                            tbLQSWD.Text = f.ToString("f1");
                            gLQSWD.Value = f;

                            f = BytesOP.MakeShort(rxList[11], rxList[12]);//AN4
                            f = 0.05 * (f - 500);
                            fdjData1.RHYWD = f;
                            CheckT1Alarm(1, 4, tbRHYWD, f);
                            tbRHYWD.Text = f.ToString("f1");
                            gRHYWD.Value = f;
                        }));
                        fdjUI1.Form2_Proc4017_1(rxList);
                    }
                    modbus.TargetAddr = 0x03;
                    if (modbus.ProcCommand04(0x0040, 0x0008, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            double f = BytesOP.MakeShort(rxList[3], rxList[4]);//AN0
                            f = 0.215 * (f - 500);
                            fdjData1.PQWD1 = f;
                            CheckT2Alarm(1, 5, txPQWD1, f);
                            txPQWD1.Text = f.ToString("f1");
                            gPQWD1.Value = f;

                            f = BytesOP.MakeShort(rxList[5], rxList[6]);//AN1
                            f = 0.215 * (f - 500);
                            fdjData1.PQWD2 = f;
                            CheckT2Alarm(1, 6, txPQWD2, f);
                            txPQWD2.Text = f.ToString("f1");
                            gPQWD2.Value = f;

                            f = BytesOP.MakeShort(rxList[7], rxList[8]);//AN2
                            f = 0.215 * (f - 500);
                            fdjData1.PQWD3 = f;
                            CheckT2Alarm(1, 7, txPQWD3, f);
                            txPQWD3.Text = f.ToString("f1");
                            gPQWD3.Value = f;

                            f = BytesOP.MakeShort(rxList[9], rxList[10]);//AN3
                            f = 0.215 * (f - 500);
                            fdjData1.PQWD4 = f;
                            CheckT2Alarm(1, 8, txPQWD4, f);
                            txPQWD4.Text = f.ToString("f1");
                            gPQWD4.Value = f;

                            f = BytesOP.MakeShort(rxList[11], rxList[12]);//AN4
                            f = 0.215 * (f - 500);
                            fdjData1.PQWD5 = f;
                            CheckT2Alarm(1, 9, txPQWD5, f);
                            txPQWD5.Text = f.ToString("f1");
                            gPQWD5.Value = f;

                            f = BytesOP.MakeShort(rxList[13], rxList[14]);//AN5
                            f = 0.215 * (f - 500);
                            fdjData1.PQWD6 = f;
                            CheckT2Alarm(1, 10, txPQWD6, f);
                            txPQWD6.Text = f.ToString("f1");
                            gPQWD6.Value = f;
                        }));
                        fdjUI1.Form2_Proc4017_2(rxList);
                    }
                }
                if(cyj2Check.Checked)
                {
                    modbus.TargetAddr = 0x01;
                    if (modbus.ProcCommand04(0x0040, 0x0008, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            double f = BytesOP.MakeShort(rxList[5], rxList[6]);//AN0
                            f = 0.0001 * (f - 500);
                            fdjData2.JYYL = f;
                            CheckYLAlarm(2, 0, tbJYYL1, f);
                            tbJYYL1.Text = f.ToString("f3");
                            gJYYL1.Value = f;

                            f = BytesOP.MakeShort(rxList[3], rxList[4]);//AN1
                            f = 0.00025 * (f - 500);
                            f = 0.1;
                            fdjData2.RYYL = f;
                            CheckYLAlarm(2, 1, tbRYYL1, f);
                            tbRYYL1.Text = f.ToString("f3");
                            gRYYL1.Value = f;

                            f = BytesOP.MakeShort(rxList[7], rxList[8]);//AN2
                            f = 0.0001 * (f - 500);
                            fdjData2.LQSYL = f;
                            CheckYLAlarm(2, 2, tbLQSYL1, f);
                            tbLQSYL1.Text = f.ToString("f3");
                            gLQSYL1.Value = f;

                            f = BytesOP.MakeShort(rxList[9], rxList[10]);//AN3
                            f = 0.05 * (f - 500);
                            fdjData2.LQSWD = f;
                            CheckT1Alarm(2, 3, tbLQSWD1, f);
                            tbLQSWD1.Text = f.ToString("f1");
                            gLQSWD1.Value = f;

                            f = BytesOP.MakeShort(rxList[11], rxList[12]);//AN4
                            f = 0.05 * (f - 500);
                            fdjData2.RHYWD = f;
                            CheckT1Alarm(2, 4, tbRHYWD1, f);
                            tbRHYWD1.Text = f.ToString("f1");
                            gRHYWD1.Value = f;
                        }));
                        fdjUI2.Form2_Proc4017_1(rxList);
                    }
                    modbus.TargetAddr = 0x02;
                    if (modbus.ProcCommand04(0x0040, 0x0008, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            double f = BytesOP.MakeShort(rxList[3], rxList[4]);//AN0
                            f = 0.215 * (f - 500);
                            fdjData2.PQWD1 = f;
                            CheckT2Alarm(2, 5, txPQWD11, f);
                            txPQWD11.Text = f.ToString("f1");
                            gPQWD11.Value = f;

                            f = BytesOP.MakeShort(rxList[5], rxList[6]);//AN1
                            f = 0.215 * (f - 500);
                            fdjData2.PQWD2 = f;
                            CheckT2Alarm(2, 6, txPQWD21, f);
                            txPQWD21.Text = f.ToString("f1");
                            gPQWD21.Value = f;

                            f = BytesOP.MakeShort(rxList[7], rxList[8]);//AN2
                            f = 0.215 * (f - 500);
                            fdjData2.PQWD3 = f;
                            CheckT2Alarm(2, 7, txPQWD31, f);
                            txPQWD31.Text = f.ToString("f1");
                            gPQWD31.Value = f;

                            f = BytesOP.MakeShort(rxList[9], rxList[10]);//AN3
                            f = 0.215 * (f - 500);
                            fdjData2.PQWD4 = f;
                            CheckT2Alarm(2, 8, txPQWD41, f);
                            txPQWD41.Text = f.ToString("f1");
                            gPQWD41.Value = f;

                            f = BytesOP.MakeShort(rxList[11], rxList[12]);//AN4
                            f = 0.215 * (f - 500);
                            fdjData2.PQWD5 = f;
                            CheckT2Alarm(2, 9, txPQWD51, f);
                            txPQWD51.Text = f.ToString("f1");
                            gPQWD51.Value = f;

                            f = BytesOP.MakeShort(rxList[13], rxList[14]);//AN5
                            f = 0.215 * (f - 500);
                            fdjData2.PQWD6 = f;
                            CheckT2Alarm(2, 10, txPQWD61, f);
                            txPQWD61.Text = f.ToString("f1");
                            gPQWD61.Value = f;
                        }));
                        fdjUI2.Form2_Proc4017_2(rxList);
                    }
                }
            }
            if(bCOM6)
            {
                RTUModbus_Master modbus1 = new RTUModbus_Master(serialPort4);
                modbus1.RxTimeOut = ModbusRxTimeOut;
                modbus1.RetryTimes = 1;
                modbus1.RetryInterval = 1;
                modbus1.TargetAddr = 0x02;
                if(cyj1Check.Checked)
                {
                    if (modbus1.ProcCommand03(23, 52, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            byte[] data = new byte[4];
                            data[0] = rxList[15 + 3];
                            data[1] = rxList[15 + 2];
                            data[2] = rxList[15 + 1];
                            data[3] = rxList[15 + 0];
                            fdjData1.Ua = BitConverter.ToSingle(data, 0);
                            tbUa.Text = fdjData1.Ua.ToString("f1");
                            gUa.Value = fdjData1.Ua;

                            data[0] = rxList[19 + 3];
                            data[1] = rxList[19 + 2];
                            data[2] = rxList[19 + 1];
                            data[3] = rxList[19 + 0];
                            fdjData1.Ub = BitConverter.ToSingle(data, 0);
                            tbUb.Text = fdjData1.Ub.ToString("f1");
                            gUb.Value = fdjData1.Ub;

                            data[0] = rxList[23 + 3];
                            data[1] = rxList[23 + 2];
                            data[2] = rxList[23 + 1];
                            data[3] = rxList[23 + 0];
                            fdjData1.Uc = BitConverter.ToSingle(data, 0);
                            tbUc.Text = fdjData1.Uc.ToString("f1");
                            gUc.Value = fdjData1.Uc;

                            data[0] = rxList[27 + 3];
                            data[1] = rxList[27 + 2];
                            data[2] = rxList[27 + 1];
                            data[3] = rxList[27 + 0];
                            fdjData1.Ia = BitConverter.ToSingle(data, 0);
                            tbIa.Text = fdjData1.Ia.ToString("f1");
                            gIa.Value = fdjData1.Ia;

                            data[0] = rxList[31 + 3];
                            data[1] = rxList[31 + 2];
                            data[2] = rxList[31 + 1];
                            data[3] = rxList[31 + 0];
                            fdjData1.Ib = BitConverter.ToSingle(data, 0);
                            tbIb.Text = fdjData1.Ib.ToString("f1");
                            gIb.Value = fdjData1.Ib;

                            data[0] = rxList[35 + 3];
                            data[1] = rxList[35 + 2];
                            data[2] = rxList[35 + 1];
                            data[3] = rxList[35 + 0];
                            fdjData1.Ic = BitConverter.ToSingle(data, 0);
                            tbIc.Text = fdjData1.Ic.ToString("f1");
                            gIc.Value = fdjData1.Ic;

                            data[0] = rxList[51 + 3];
                            data[1] = rxList[51 + 2];
                            data[2] = rxList[51 + 1];
                            data[3] = rxList[51 + 0];
                            fdjData1.Power = BitConverter.ToSingle(data, 0);
                            fdjData1.Power = fdjData1.Power / 1000;
                            tbP.Text = fdjData1.Power.ToString("f1");

                            data[0] = rxList[99 + 3];
                            data[1] = rxList[99 + 2];
                            data[2] = rxList[99 + 1];
                            data[3] = rxList[99 + 0];
                            fdjData1.allQ = BitConverter.ToSingle(data, 0);
                            tbQ.Text = fdjData1.allQ.ToString("f1");

                            data[0] = rxList[103 + 3];
                            data[1] = rxList[103 + 2];
                            data[2] = rxList[103 + 1];
                            data[3] = rxList[103 + 0];
                            fdjData1.freq = BitConverter.ToSingle(data, 0);
                            tbF.Text = fdjData1.freq.ToString("f1");
                        }));
                        fdjUI1.Form2_ProcDB(rxList);
                    }
                }
                if(cyj2Check.Checked)
                {
                    modbus1.TargetAddr = 0x01;
                    if (modbus1.ProcCommand03(23, 52, out rxList))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            byte[] data = new byte[4];
                            data[0] = rxList[15 + 3];
                            data[1] = rxList[15 + 2];
                            data[2] = rxList[15 + 1];
                            data[3] = rxList[15 + 0];
                            fdjData2.Ua = BitConverter.ToSingle(data, 0);
                            tbUa1.Text = fdjData2.Ua.ToString("f1");
                            gUa1.Value = fdjData2.Ua;

                            data[0] = rxList[19 + 3];
                            data[1] = rxList[19 + 2];
                            data[2] = rxList[19 + 1];
                            data[3] = rxList[19 + 0];
                            fdjData2.Ub = BitConverter.ToSingle(data, 0);
                            tbUb1.Text = fdjData2.Ub.ToString("f1");
                            gUb1.Value = fdjData2.Ub;

                            data[0] = rxList[23 + 3];
                            data[1] = rxList[23 + 2];
                            data[2] = rxList[23 + 1];
                            data[3] = rxList[23 + 0];
                            fdjData2.Uc = BitConverter.ToSingle(data, 0);
                            tbUc1.Text = fdjData2.Uc.ToString("f1");
                            gUc1.Value = fdjData2.Uc;

                            data[0] = rxList[27 + 3];
                            data[1] = rxList[27 + 2];
                            data[2] = rxList[27 + 1];
                            data[3] = rxList[27 + 0];
                            fdjData2.Ia = BitConverter.ToSingle(data, 0);
                            tbIa1.Text = fdjData2.Ia.ToString("f1");
                            gIa1.Value = fdjData2.Ia;

                            data[0] = rxList[31 + 3];
                            data[1] = rxList[31 + 2];
                            data[2] = rxList[31 + 1];
                            data[3] = rxList[31 + 0];
                            fdjData2.Ib = BitConverter.ToSingle(data, 0);
                            tbIb1.Text = fdjData2.Ib.ToString("f1");
                            gIb1.Value = fdjData2.Ib;

                            data[0] = rxList[35 + 3];
                            data[1] = rxList[35 + 2];
                            data[2] = rxList[35 + 1];
                            data[3] = rxList[35 + 0];
                            fdjData2.Ic = BitConverter.ToSingle(data, 0);
                            tbIc1.Text = fdjData2.Ic.ToString("f1");
                            gIc1.Value = fdjData2.Ic;

                            data[0] = rxList[51 + 3];
                            data[1] = rxList[51 + 2];
                            data[2] = rxList[51 + 1];
                            data[3] = rxList[51 + 0];
                            fdjData2.Power = BitConverter.ToSingle(data, 0);
                            fdjData2.Power = fdjData2.Power / 1000;
                            tbP1.Text = fdjData2.Power.ToString("f1");

                            data[0] = rxList[99 + 3];
                            data[1] = rxList[99 + 2];
                            data[2] = rxList[99 + 1];
                            data[3] = rxList[99 + 0];
                            fdjData2.allQ = BitConverter.ToSingle(data, 0);
                            tbQ1.Text = fdjData2.allQ.ToString("f1");

                            data[0] = rxList[103 + 3];
                            data[1] = rxList[103 + 2];
                            data[2] = rxList[103 + 1];
                            data[3] = rxList[103 + 0];
                            fdjData2.freq = BitConverter.ToSingle(data, 0);
                            tbF1.Text = fdjData2.freq.ToString("f1");
                        }));
                        fdjUI2.Form2_ProcDB(rxList);
                    }
                }
            }
            if (bCOM5)
            {
                modbus.TargetAddr = 0x01;
                if(cyj1Check.Checked)
                {                    
                    bool bAlarmNow1 = false;
                    if (AlarmByte1 != 0)
                        bAlarmNow1 = true;
                    if (bAlarmNow1 != bAlarm1)
                    {
                        if (bAlarmNow1)
                        {
                            modbus.ProcCommand05(0x0200, 0x0000);
                            bAlarm1 = true;
                        }
                        else
                        {
                            modbus.ProcCommand05(0x0200, 0xff00);
                            bAlarm1 = false;
                        }
                    }
                    if (bAlarmNow1)
                    {
                        if (bAlarmSave1)
                        {
                            bAlarmSave1 = false;
                            timer3.Enabled = true;
                            fdjData1.GetTime = DateTime.Now;
                            fdjDataDBOption.Insert(fdjData1, "fdj1Alarm");
                        }
                    }
                }
                else //if (bAlarm1)
                {
                    modbus.ProcCommand05(0x0200, 0xff00);
                    bAlarm1 = false;
                }                
                modbus.TargetAddr = 0x04;
                if(cyj2Check.Checked)
                {                    
                    bool bAlarmNow2 = false;
                    if (AlarmByte2 != 0)
                        bAlarmNow2 = true;
                    if (bAlarmNow2 != bAlarm2)
                    {
                        if (bAlarmNow2)
                        {
                            modbus.ProcCommand05(0x0200, 0x0000);
                            bAlarm2 = true;
                        }
                        else
                        {
                            modbus.ProcCommand05(0x0200, 0xff00);
                            bAlarm2 = false;
                        }
                    }
                    if (bAlarmNow2)
                    {
                        if (bAlarmSave2)
                        {
                            bAlarmSave2 = false;
                            timer4.Enabled = true;
                            fdjData2.GetTime = DateTime.Now;
                            fdjDataDBOption.Insert(fdjData2, "fdj2Alarm");
                        }
                    }
                }
                else// if (bAlarm2)
                {
                    modbus.ProcCommand05(0x0200, 0xff00);
                    bAlarm2 = false;
                }
            }
            timer2.Enabled = true;
        }
        static ushort AlarmByte1=0;
        static ushort AlarmByte2=0;
        public void CheckYLAlarm(int Num,int index,Label ctrl, double data)
        {
            switch (index)
            {
                case 0://机油
                    if (Num == 1)
                    {
                        if (data < 0.1)
                        {
                            //ctrl.ForeColor = Color.Blue;
                            BytesOP.SetBit(ref AlarmByte1, index);
                        }
                        else if (data > 0.16)
                        {
                            //ctrl.ForeColor = Color.Red;
                            BytesOP.SetBit(ref AlarmByte1, index);                    
                        }
                        else
                        {
                            //ctrl.ForeColor = Color.Black;
                            BytesOP.ClrBit(ref AlarmByte1, index);
                        }
                    }
                    if (Num == 2)
                    {
                        if (data < 0.1)
                        {
                            //ctrl.ForeColor = Color.Blue;
                            BytesOP.SetBit(ref AlarmByte2, index);
                        }
                        else if (data > 0.16)
                        {
                            //ctrl.ForeColor = Color.Red;
                            BytesOP.SetBit(ref AlarmByte2, index);
                        }
                        else
                        {
                            //ctrl.ForeColor = Color.Black;
                            BytesOP.ClrBit(ref AlarmByte2, index);
                        }
                    } 
                    break;
                case 1://燃油
                    if (Num == 1)
                    {
                        if (data ==0)//< 0.05)
                        {
                            //ctrl.ForeColor = Color.Blue;
                            BytesOP.SetBit(ref AlarmByte1, index);
                        }                       
                        else
                        {
                            //ctrl.ForeColor = Color.Black;
                            BytesOP.ClrBit(ref AlarmByte1, index);
                        }
                    }
                    if (Num == 2)
                    {
                        if (data ==0)
                        {
                            //ctrl.ForeColor = Color.Blue;
                            BytesOP.SetBit(ref AlarmByte2, index);
                        }
                        else
                        {
                            //ctrl.ForeColor = Color.Black;
                            BytesOP.ClrBit(ref AlarmByte2, index);
                        }
                    }
                    break;
                case 2://冷却水
                    if (Num == 1)
                    {
                        if (data < 0.05)
                        {
                            //ctrl.ForeColor = Color.Blue;
                            BytesOP.SetBit(ref AlarmByte1, index);
                        }
                        else
                        {
                            //ctrl.ForeColor = Color.Black;
                            BytesOP.ClrBit(ref AlarmByte1, index);
                        }
                    }
                    if (Num == 2)
                    {
                        if (data < 0.05)
                        {
                            //ctrl.ForeColor = Color.Blue;
                            BytesOP.SetBit(ref AlarmByte2, index);
                        }
                        else
                        {
                            //ctrl.ForeColor = Color.Black;
                            BytesOP.ClrBit(ref AlarmByte2, index);
                        }
                    }
                    break;
            }                   
        }
        public void CheckT1Alarm(int Num, int index, Label ctrl, double data)
        {
            if (Num == 1)
            {
                if (data > 75)
                {
                    //ctrl.ForeColor = Color.Red;
                    BytesOP.SetBit(ref AlarmByte1, index);
                }
                else
                {
                    //ctrl.ForeColor = Color.Black;
                    BytesOP.ClrBit(ref AlarmByte1, index);
                }
            }
            if (Num == 2)
            {
                if (data > 75)
                {
                    //ctrl.ForeColor = Color.Red;
                    BytesOP.SetBit(ref AlarmByte2, index);
                }
                else
                {
                    //ctrl.ForeColor = Color.Black;
                    BytesOP.ClrBit(ref AlarmByte2, index);
                }
            }
        }
        public void CheckT2Alarm(int Num, int index, Label ctrl, double data)
        {
            if (Num == 1)
            {
                if (data > 420)
                {
                    //ctrl.ForeColor = Color.Red;
                    BytesOP.SetBit(ref AlarmByte1, index);
                }
                else
                {
                    //ctrl.ForeColor = Color.Black;
                    BytesOP.ClrBit(ref AlarmByte1, index);
                }
            }
            if (Num == 2)
            {
                if (data > 420)
                {
                    //ctrl.ForeColor = Color.Red;
                    BytesOP.SetBit(ref AlarmByte2, index);
                }
                else
                {
                    //ctrl.ForeColor = Color.Black;
                    BytesOP.ClrBit(ref AlarmByte2, index);
                }
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            timer3.Enabled = false;
            bAlarmSave1 = true;
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            timer4.Enabled = false;
            bAlarmSave2 = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            fdjData1.GetTime = DateTime.Now;
            fdjDataDBOption.Insert(fdjData1, "fdj1data");
            MessageBox.Show("发电机1数据记录成功");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            fdjData2.GetTime = DateTime.Now;
            fdjDataDBOption.Insert(fdjData2, "fdj2data");
            MessageBox.Show("发电机2数据记录成功");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form4 f = new Form4();
            f.ShowDialog();
        }

        private void label36_Click(object sender, EventArgs e)
        {

        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void label19_Click(object sender, EventArgs e)
        {

        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void gZS_BeforeChangeValue(object sender, NationalInstruments.UI.BeforeChangeNumericValueEventArgs e)
        {
            Gauge f = (Gauge)sender;
            if (e.NewValue > f.Range.Maximum)
            {
                f.Value = f.Range.Maximum;
                e.Cancel = true;
            }
            else if (e.NewValue < f.Range.Minimum)
            {
                f.Value = f.Range.Minimum;
                e.Cancel = true;
            }
        }

        private void cyj1Check_CheckedChanged(object sender, EventArgs e)
        {
            AlarmByte1 = 0;
            if (cyj1Check.Checked)
            {
                cyj1Check.Text = "一号发电机组开启";               
            }
            else
            {
                cyj1Check.Text = "一号发电机组关闭";
            }
        }

        private void cyj2Check_CheckedChanged(object sender, EventArgs e)
        {
            AlarmByte2 = 0;
            if (cyj2Check.Checked)
            {
                cyj2Check.Text = "二号发电机组开启";
            }
            else
            {
                cyj2Check.Text = "二号发电机组关闭";
            }
        }
    }
}
